package com.ocom.webapi.controller;

import com.ocom.common.resp.Result;
import com.ocom.common.utils.NumConvertUtil;
import com.ocom.common.utils.RandomUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.oauth.SsoVo;
import com.ocom.redis.util.RedisUtil;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2023/5/8
 **/
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
@RestController
public class SsoController {

	@Resource
	private  RedisUtil redisUtil;


	@PostMapping("/banpaiSsoCode")
	public Result getCode() {

		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();

		Authentication yoCiUser222 =  SecurityUtils.getAuthentication();


//		System.out.println(yoCiUser222.getPrincipal());
//		System.out.println(yoCiUser222.getDetails());
//		System.out.println(yoCiUser222.getCredentials());

		String code = "XAK"+ NumConvertUtil.IntLegth(yoCiUser.getComId().intValue(),6)+RandomUtil.getRandomString(20)+ System.currentTimeMillis();

		String mqKey = "SsoCode:webSsoCode:"+ code;

		SsoVo ssoVo= new SsoVo();

		ssoVo.setComId(yoCiUser.getComId());
		ssoVo.setUserId(yoCiUser.getId());
		ssoVo.setUserName(yoCiUser.getUsername());

		//1天
		ssoVo.setExpire(System.currentTimeMillis()+86400000);

		redisUtil.set(mqKey,ssoVo,300l);

		return ResultUtil.success(code);
	}
}
